<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="项目编号" prop="projectNumber">
        <template>
          <el-select v-model="dataForm.projectNumber" :disabled="dataForm.id != 0" placeholder="请选择"
                     @change="getProductInfo()">
            <el-option
                v-for="item in options"
                :key="item.prProjectNumber"
                :label="item.prProjectName"
                :value="item.prProjectNumber">
            </el-option>
          </el-select>
        </template>
        <!--      <el-input v-model="dataForm.projectNumber" placeholder="项目编号"></el-input>-->
      </el-form-item>
      <el-form-item label="产品名称" prop="product">
        <el-input v-model="dataForm.product" placeholder="产品名称"></el-input>
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input v-model="dataForm.model" placeholder="型号"></el-input>
      </el-form-item>
      <el-form-item label="企业名称" prop="companyName">
        <template>
          <el-select v-model="dataForm.companyName" placeholder="请选择" @change="getEntrepreneurInfo()  ">
            <el-option
                v-for="item in eoptions"
                :key="item.id"
                :label="item.name"
                :value="item.name">
            </el-option>
          </el-select>
        </template>
        <!--      <el-input v-model="dataForm.companyName" placeholder="企业名称"></el-input>-->
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="contact">
        <el-input v-model="dataForm.contact" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="鉴定依据" prop="identificationBasis">
        <el-input v-model="dataForm.identificationBasis" placeholder="鉴定依据"></el-input>
      </el-form-item>
      <el-form-item label="抽样/验样" prop="samplingSample">
        <template>
          <el-radio v-model="dataForm.samplingSample" label="1">抽样</el-radio>
          <el-radio v-model="dataForm.samplingSample" label="0">验样</el-radio>
        </template>
      </el-form-item>
      <el-form-item label="抽样基数" prop="samplingBasic" v-if="dataForm.samplingSample==='1'">
        <el-input v-model="dataForm.samplingBasic" placeholder="抽样数量"></el-input>
      </el-form-item>
      <el-form-item label="抽样数量" prop="samplingAmount" v-if="dataForm.samplingSample==='1'">
        <el-input v-model="dataForm.samplingAmount" placeholder="抽样数量"></el-input>
      </el-form-item>
      <el-form-item label="完成期限" prop="deadline">
        <el-date-picker
            v-model="dataForm.deadline"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="项目负责人" prop="projectLeader">
        <el-input v-model="dataForm.projectLeader" placeholder="项目负责人"></el-input>
      </el-form-item>
      <el-form-item label="参加人" prop="participant">
        <el-input v-model="dataForm.participant" placeholder="参加人"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        projectNumber: '',
        projectType: '',
        product: '',
        model: '',
        companyName: '',
        address: '',
        contact: '',
        phone: '',
        identificationBasis: '',
        taskDefinition: '',
        samplingSample: '',
        samplingAmount: '',
        samplingBasic:'',
        deadline: '',
        projectLeader: '',
        participant: '',
        responsibleOpinion: '',
        signaturePerson: '',
        responsibleTime: '',
        opinionDirector: '',
        signatureLeader: '',
        opinionTime: '',
        mastersInstructions: '',
        signatureInstructions: '',
        instructionsTime: ''
      },
      options: [],
      eoptions: [],
      productInfo: {},
      dataRule: {
        projectNumber: [
          {required: true, message: '项目编号不能为空', trigger: 'blur'}
        ],
        projectType: [
          {required: true, message: '检验类别不能为空', trigger: 'blur'}
        ],
        product: [
          {required: true, message: '产品名称不能为空', trigger: 'blur'}
        ],
        model: [
          {required: true, message: '型号不能为空', trigger: 'blur'}
        ],
        companyName: [
          {required: true, message: '企业名称不能为空', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '地址不能为空', trigger: 'blur'}
        ],
        contact: [
          {required: true, message: '联系人不能为空', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '电话不能为空', trigger: 'blur'}
        ],
        identificationBasis: [
          {required: true, message: '鉴定依据不能为空', trigger: 'blur'}
        ],
        taskDefinition: [
          {required: true, message: '任务内容不能为空', trigger: 'blur'}
        ],
        samplingSample: [
          {required: true, message: '抽样/验样不能为空', trigger: 'blur'}
        ],
        samplingAmount: [
          {required: true, message: '抽样数量不能为空', trigger: 'blur'}
        ],
        deadline: [
          {required: true, message: '完成期限不能为空', trigger: 'blur'}
        ],
        projectLeader: [
          {required: true, message: '项目负责人不能为空', trigger: 'blur'}
        ],
        participant: [
          {required: true, message: '参加人不能为空', trigger: 'blur'}
        ],
        responsibleOpinion: [
          {required: true, message: '负责人意见不能为空', trigger: 'blur'}
        ],
        signaturePerson: [
          {required: true, message: '负责人签名不能为空', trigger: 'blur'}
        ],
        responsibleTime: [
          {required: true, message: '负责人时间不能为空', trigger: 'blur'}
        ],
        opinionDirector: [
          {required: true, message: '技术负责人意见不能为空', trigger: 'blur'}
        ],
        signatureLeader: [
          {required: true, message: '技术负责人签名不能为空', trigger: 'blur'}
        ],
        opinionTime: [
          {required: true, message: '技术负责人时间不能为空', trigger: 'blur'}
        ],
        mastersInstructions: [
          {required: true, message: '站长批示不能为空', trigger: 'blur'}
        ],
        signatureInstructions: [
          {required: true, message: '站长签名不能为空', trigger: 'blur'}
        ],
        instructionsTime: [
          {required: true, message: '站长时间不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  mounted() {
    this.getProductNumber();
    this.geteoptions()
  },
  methods: {
    geteoptions() {
      this.$http({
        url: this.$http.adornUrl('/reportapproval/entrepreneur/geteoptions'),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        if (data && data.code === 0) {
          this.eoptions = data.entrepreneurList
          console.log(data.entrepreneurList)
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    getEntrepreneurInfo() {
      this.$http({
        url: this.$http.adornUrl(`/reportapproval/entrepreneur/getCompanyNameInfo/${this.dataForm.companyName}`),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        if (data && data.code === 0) {
          this.dataForm.address = data.entrepreneur.address
          this.dataForm.contact = data.entrepreneur.linkman
          this.dataForm.phone = data.entrepreneur.linkphone
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    getProductNumber() {
      this.$http({
        url: this.$http.adornUrl('/reportapproval/product/getProjectNumber'),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        if (data && data.code === 0) {
          this.options = data.productList
          // console.log(this.options)
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    getProductInfo() {
      this.$http({
        url: this.$http.adornUrl(`/reportapproval/product/getProductInfo/${this.dataForm.projectNumber}`),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        if (data && data.code === 0) {
          this.dataForm.product = data.productInfo.prProjectName
          this.dataForm.model = data.productInfo.prProducerModel

        } else {
          this.$message.error(data.msg)
        }
      })
    },
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/reportapproval/authenticationtask/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then((data) => {
            if (data && data.code === 0) {
              this.dataForm.projectNumber = data.authenticationTask.projectNumber
              this.dataForm.projectType = data.authenticationTask.projectType
              this.dataForm.product = data.authenticationTask.product
              this.dataForm.model = data.authenticationTask.model
              this.dataForm.companyName = data.authenticationTask.companyName
              this.dataForm.address = data.authenticationTask.address
              this.dataForm.contact = data.authenticationTask.contact
              this.dataForm.phone = data.authenticationTask.phone
              this.dataForm.identificationBasis = data.authenticationTask.identificationBasis
              this.dataForm.taskDefinition = data.authenticationTask.taskDefinition
              this.dataForm.samplingSample = data.authenticationTask.samplingSample
              this.dataForm.samplingBasic = data.authenticationTask.samplingBasic
              this.dataForm.samplingAmount = data.authenticationTask.samplingAmount
              this.dataForm.deadline = data.authenticationTask.deadline
              this.dataForm.projectLeader = data.authenticationTask.projectLeader
              this.dataForm.participant = data.authenticationTask.participant
              this.dataForm.responsibleOpinion = data.authenticationTask.responsibleOpinion
              this.dataForm.signaturePerson = data.authenticationTask.signaturePerson
              this.dataForm.responsibleTime = data.authenticationTask.responsibleTime
              this.dataForm.opinionDirector = data.authenticationTask.opinionDirector
              this.dataForm.signatureLeader = data.authenticationTask.signatureLeader
              this.dataForm.opinionTime = data.authenticationTask.opinionTime
              this.dataForm.mastersInstructions = data.authenticationTask.mastersInstructions
              this.dataForm.signatureInstructions = data.authenticationTask.signatureInstructions
              this.dataForm.instructionsTime = data.authenticationTask.instructionsTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/reportapproval/authenticationtask/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'projectNumber': this.dataForm.projectNumber,
              'projectType': this.dataForm.projectType,
              'product': this.dataForm.product,
              'model': this.dataForm.model,
              'companyName': this.dataForm.companyName,
              'address': this.dataForm.address,
              'contact': this.dataForm.contact,
              'phone': this.dataForm.phone,
              'identificationBasis': this.dataForm.identificationBasis,
              'taskDefinition': this.dataForm.taskDefinition,
              'samplingSample': this.dataForm.samplingSample,
              'samplingAmount': this.dataForm.samplingAmount,
              'deadline': this.dataForm.deadline,
              'projectLeader': this.dataForm.projectLeader,
              'participant': this.dataForm.participant,
              'responsibleOpinion': this.dataForm.responsibleOpinion,
              'signaturePerson': this.dataForm.signaturePerson,
              'responsibleTime': this.dataForm.responsibleTime,
              'opinionDirector': this.dataForm.opinionDirector,
              'signatureLeader': this.dataForm.signatureLeader,
              'opinionTime': this.dataForm.opinionTime,
              'mastersInstructions': this.dataForm.mastersInstructions,
              'signatureInstructions': this.dataForm.signatureInstructions,
              'instructionsTime': this.dataForm.instructionsTime
            })
          }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
